﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sunboxsoft.Common;
using System.Data.SQLite;

namespace Sunboxsoft.ASMS.Client.Domain.Service
{
    public class CreateService
    {
        /// <summary>
        /// 创建客户资料
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public SingleResult<WebRef.ECustomer> CreateCustomer(WebRef.ECustomer customer)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                service.Url = RemoteInfo.ASMSWebServiceURL;
                WebRef.SingleResultOfECustomer result = service.CreateCustomer(customer);
                if (result.IsSuccess)
                {
                    return new SingleResult<WebRef.ECustomer>(result.Data);
                }
                return new SingleResult<WebRef.ECustomer>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.ECustomer>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建预约单
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="servies"></param>
        /// <returns></returns>
        public ServiceResult CreateBookingBill(WebRef.EBookingBill bill, WebRef.EBookingService[] servies)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateBookingBill(bill, servies);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建汽车信息
        /// </summary>
        /// <param name="automobile"></param>
        /// <returns></returns>
        public ServiceResult CreateAutomobile(WebRef.EAutomobile automobile)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateAutomobile(automobile);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建付费项目
        /// </summary>
        /// <param name="payItem"></param>
        /// <returns></returns>
        [Obsolete("不使用")]
        public ServiceResult CreatePayItem(WebRef.EPayItem payItem)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreatePayItem(payItem);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建商品
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        [Obsolete("不使用")]
        public SingleResult<WebRef.EMaterial> CreateMaterial(WebRef.EMaterial material)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.SingleResultOfEMaterial result = service.CreateMaterial(material);
                if (result.IsSuccess)
                {
                    return new SingleResult<WebRef.EMaterial>(result.Data);
                }
                return new SingleResult<WebRef.EMaterial>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EMaterial>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建仓库
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public ServiceResult CreateWarehouse(WebRef.EWarehouse warehouse)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateWarehouse(warehouse);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建库存入库单
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="itemArray"></param>
        /// <returns></returns>
        public ServiceResult CreateStockIncreaseBill(WebRef.EStockIncreaseBill bill, WebRef.EStockIncreaseBillItem[] itemArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateStockIncreaseBill(bill, itemArray);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建出库单
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="itemArray"></param>
        /// <returns></returns>
        public ServiceResult CreateStockDecreaseBill(WebRef.EStockDecreaseBill bill, WebRef.EStockDecreaseBillItem[] itemArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateStockDecreaseBill(bill, itemArray);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建库存报警值
        /// </summary>
        /// <param name="stockWarning"></param>
        /// <returns></returns>
        public ServiceResult CreateStockThresholdValue(WebRef.EStockThresholdValue thresholdValue)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateStockThresholdValue(thresholdValue);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [Obsolete("不使用")]
        public ServiceResult CreateSupplier(WebRef.ESupplier supplier)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateSupplier(supplier);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建维修服务单
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="itemArray"></param>
        /// <param name="materialArray"></param>
        /// <param name="payItemArray"></param>
        /// <returns></returns>
        public SingleResult<WebRef.EMaintenanceBill> CreateMaintenanceBill(WebRef.EMaintenanceBill bill, WebRef.EMaintenanceBillServiceItem[] itemArray,
            WebRef.EMaintenanceBillMaterial[] materialArray,
            WebRef.EMaintenanceBillPayItem[] payItemArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.SingleResultOfEMaintenanceBill result = service.CreateMaintenanceBill(bill, itemArray, materialArray, payItemArray);
                if (result.IsSuccess)
                {
                    return new SingleResult<WebRef.EMaintenanceBill>(result.Data);
                }
                return new SingleResult<WebRef.EMaintenanceBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EMaintenanceBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        #region package

        /// <summary>
        /// 创建套餐购买单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public SingleResult<WebRef.EPackagePurchaseBill> CreatePackagePurchaseBill(WebRef.EPackagePurchaseBill bill)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.SingleResultOfEPackagePurchaseBill result = service.CreatePackagePurchaseBill(bill);
                if (result.IsSuccess)
                {
                    return new SingleResult<WebRef.EPackagePurchaseBill>(result.Data);
                }
                return new SingleResult<WebRef.EPackagePurchaseBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EPackagePurchaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建套餐
        /// </summary>
        /// <param name="package"></param>
        /// <param name="priceArray"></param>
        /// <param name="itemArray"></param>
        /// <param name="scopeArray"></param>
        /// <returns></returns>
        public ServiceResult CreatePackage(WebRef.EPackage package, WebRef.ESalesPrice[] priceArray, WebRef.EPackageItem[] itemArray, WebRef.EPackageScope[] scopeArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreatePackage(package, priceArray, itemArray, scopeArray);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EPackagePurchaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        #endregion

        /// <summary>
        /// 创建派工单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public ServiceResult CreateWorkingBill(WebRef.EWorkingBill bill)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateWorkingBill(bill);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EPackagePurchaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建领料单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public ServiceResult CreateMaterialRequisitionBill(WebRef.EMaterialRequisitionBill bill)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateMaterialRequisitionBill(bill);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EPackagePurchaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }


        /// <summary>
        /// 创建退料单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public ServiceResult CreateMaterialReturnedBill(WebRef.EMaterialReturnedBill bill)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateMaterialReturnedBill(bill);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<WebRef.EPackagePurchaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        #region settlement

        /// <summary>
        /// 创建结算单
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="serviceItemArray">服务项</param>
        /// <param name="materailArray">物料</param>
        /// <param name="payItemArray">付费项目</param>
        /// <param name="packageItemArray">套餐</param>
        /// <param name="couponArray">优惠券</param>
        /// <returns></returns>
        public SingleResult<Guid> CreateSettlementBill(WebRef.ESettlementBill bill,
            WebRef.ESettlementBillServiceItem[] serviceItemArray,
            WebRef.ESettlementBillMaterial[] materailArray,
            WebRef.ESettlementBillPayItem[] payItemArray,
            WebRef.ESettlementBillPackageItem[] packageItemArray,
            WebRef.ESettlementBillCoupon[] couponArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.SingleResultOfGuid result = service.CreateSettlementBill(bill, serviceItemArray, materailArray, payItemArray, packageItemArray, couponArray);
                if (result.IsSuccess)
                {
                    return new SingleResult<Guid>(result.Data);
                }
                return new SingleResult<Guid>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<Guid>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
        #endregion

        #region recharge

        /// <summary>
        /// 创建充值项目
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public ServiceResult CreateRechargeItem(WebRef.ERechargeItem item)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateRechargeItem(item);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建充值单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public SingleResult<Guid> CreateRechargeBill(WebRef.ERechargeBill bill)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.SingleResultOfGuid result = service.CreateRechargeBill(bill);
                if (result.IsSuccess)
                {
                    return new SingleResult<Guid>(result.Data);
                }
                return new SingleResult<Guid>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new SingleResult<Guid>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
        #endregion

        /// <summary>
        /// 创建服务项目
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public ServiceResult CreateServiceItem(WebRef.EServiceItem item)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateServiceItem(item);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建服务项目类型
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public ServiceResult CreateServiceCategory(WebRef.EServiceCategory category)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateServiceCategory(category);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建资金入账单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public ServiceResult CreateCapticalIncreaseBill(WebRef.ECapitalIncreaseBill bill, WebRef.EPayRecord[] payRecordArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateCapticalIncreaseBill(bill, payRecordArray);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 创建资金出账单
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="payRecordArray"></param>
        /// <returns></returns>
        public ServiceResult CreateCapticalDecreaseBill(WebRef.ECapitalDecreaseBill bill, WebRef.EPayRecord[] payRecordArray)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ServiceResult result = service.CreateCapticalDecreaseBill(bill, payRecordArray);
                if (result.IsSuccess)
                {
                    return new ServiceResult();
                }
                return new ServiceResult(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ServiceResult(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
    }
}
